package org.phalanx.server.ServerCache;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

/*
 * Author: Steven Day
 * 
 * This class will be used as a generic container
 * for values returned from a data source (such as SQL).
 * 
 * When a query is done QueryObject will hold all the values
 * as well as the key (SQL column name) 
 * 
 * 
 * */

public class QueryObject {
	Hashtable<String,Object> queryObjectTable;
	
	public QueryObject(){
		queryObjectTable = new Hashtable<String,Object>();
	}
	
	public void setString(String key, String value){
		if (key != null && value != null)
			queryObjectTable.put(key, value);
	}
	
	public void setInteger(String key, Integer value){
		if (key != null && value != null)
			queryObjectTable.put(key, value);
	}
	
	public void setDouble(String key, Double value){
		if (key != null && value != null)
			queryObjectTable.put(key, value);
	}
	
	public void setBoolean(String key, Boolean value){
		if (key != null && value != null)
			queryObjectTable.put(key, value);
	}
	
	public String getString(String key){
		String valueObject = null;
		
		if (queryObjectTable.containsKey(key))
			valueObject = queryObjectTable.get(key).toString();
		
		return valueObject;
	}
	
	public Integer getInteger(String key){
		Integer intValue = null;
		Object genericValue = queryObjectTable.get(key);

		if (genericValue != null){
			try {
				intValue = Integer.valueOf(genericValue.toString());
			} catch (NumberFormatException nfe){
				nfe.printStackTrace();
			}
		}
		return intValue;
	}
	
	public Double getDouble(String key){
		Double dblValue = null;
		Object genericValue = queryObjectTable.get(key);
		
		if (genericValue != null){
			try {
				dblValue = Double.valueOf(genericValue.toString());
			} catch (NumberFormatException nfe){
				nfe.printStackTrace();
			}
		}
		return dblValue;
	}
	
	public Boolean getBoolean(String key){
		Boolean boolValue = null;
		Object genericValue = queryObjectTable.get(key);
		
		if (genericValue != null){
			boolValue = genericValue.toString().equalsIgnoreCase("yes") ? true : false;
		}
		
		return boolValue;
	}
	
	public Long getLong(String key){
		Long longValue = null;
		Object genericValue = queryObjectTable.get(key);
		
		if (genericValue != null){
			try {
				longValue = Long.valueOf(genericValue.toString());
			} catch (NumberFormatException nfe){
				nfe.printStackTrace();
			}
		}
		return longValue;
	}
	
	public Timestamp getTimeStamp(String key){
		Timestamp timestampValue = null;
		Object genericValue = queryObjectTable.get(key);
		
		if (genericValue != null){
			try {
				timestampValue = Timestamp.valueOf(genericValue.toString());
			} catch (NumberFormatException nfe){
				nfe.printStackTrace();
			}
		}
		return timestampValue;
	}
	
	public ArrayList<String> getColumns(){
		ArrayList<String> columns = new ArrayList<String>();
		
		Enumeration<String> keysAsColumns = queryObjectTable.keys();
		
		while(keysAsColumns.hasMoreElements()){
			columns.add(keysAsColumns.nextElement());
		}
		
		return columns;
	}
}
